package com.godpaper.the5elements.busniess.managers
{
	import com.godpaper.the5elements.busniess.AI.RandomWalkAI;
	import com.godpaper.the5elements.model.BoardModel;
	import com.godpaper.the5elements.model.PiecesModel;
	import com.godpaper.the5elements.model.PlayerModel;
	import com.godpaper.the5elements.views.components.PieceButton;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.logging.ILogger;
	import mx.logging.Log;

	public class PlayerManager
	{
		private static const log:ILogger = mx.logging.Log.getLogger("PlayerManager");
		
		public function PlayerManager()
		{
		}
		
		public static function computerWin():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
			mx.controls.Alert.show("COMPUTER WIN!");
		}
		public static function humanWin():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
			mx.controls.Alert.show("HUMAN WIN!");
		}
		
		private static var gameAI:RandomWalkAI;
		public static function isComputerTurnNow():void
		{
			ProgressBarManager.getInstance().showProgressBar();
			
			PlayerModel.getInstance().gameTurnStatus = !PlayerModel.getInstance().gameTurnStatus;
			
			for(var b:int=0;b<PiecesModel.getInstance().bluePiecesCollection.length;b++)
			{
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(b) as PieceButton).mouseChildren = false;
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(b) as PieceButton).mouseEnabled = false;
			}	
			for(var r:int=0;r<PiecesModel.getInstance().redPiecesCollection.length;r++)
			{
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(r) as PieceButton).mouseChildren = true;
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(r) as PieceButton).mouseEnabled = true;
			}			
			//todo:
			gameAI = new RandomWalkAI(BoardModel.getInstance().gamePosition);
		}
		public static function isHumanTurnNow():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
			
			PlayerModel.getInstance().gameTurnStatus = !PlayerModel.getInstance().gameTurnStatus;
			//todo:
			for(var b:int=0;b<PiecesModel.getInstance().bluePiecesCollection.length;b++)
			{
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(b) as PieceButton).mouseChildren = true;
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(b) as PieceButton).mouseEnabled = true;
			}	
			for(var r:int=0;r<PiecesModel.getInstance().redPiecesCollection.length;r++)
			{
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(r) as PieceButton).mouseChildren = false;
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(r) as PieceButton).mouseEnabled = false;
			}		
		}
		//whether game ended or some pieces beKilled.
		public static function analysisGame(reds:ArrayCollection,blues:ArrayCollection):Boolean
		{
			log.info("analysisGame()");
			if(reds.length==0)
			{
				computerWin();
			}else if(blues.length==0)
			{
				humanWin();
			}
			log.info("~analysisGame()");
			return (reds.length==0 || blues.length==0);
		}
		
	}
}